﻿#region License
// Copyright 2009 Buu Nguyen (http://www.buunguyen.net/blog)
// 
// Licensed under the Apache License, Version 2.0 (the "License"); 
// you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0 
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
// See the License for the specific language governing permissions and 
// limitations under the License.
// 
// The latest version of this file can be found at http://dotcommon.codeplex.com
#endregion

using System.Collections.Generic;
using System.Xml.Linq;
using DotCommon.Exception;

namespace DotCommon.Web
{
    /// <summary>
    /// Helper class to send code optimization request to Google Closure REST service
    /// and return the optimized code.
    /// </summary>
    public class ClosureCodeRequest
    {
        public enum CompilationLevel
        {
            Simple,
            Advanced
        }

        private const string DefaultApiUrl = "http://closure-compiler.appspot.com/compile";

        private readonly Request request;

        public ClosureCodeRequest(string jsContent) 
            : this(DefaultApiUrl, CompilationLevel.Simple, jsContent)
        {
        }

        public ClosureCodeRequest(CompilationLevel compilationLevel, string jsContent)
            : this(DefaultApiUrl, compilationLevel, jsContent)
        {
        }

        public ClosureCodeRequest(string apiUrl, string jsContent)
            : this(apiUrl, CompilationLevel.Simple, jsContent)
        {
        }

        public ClosureCodeRequest(string apiUrl, CompilationLevel compilationLevel, string jsContent)
        {
            var level = compilationLevel == CompilationLevel.Simple
                            ? "SIMPLE_OPTIMIZATIONS"
                            : "ADVANCED_OPTIMIZATIONS";
            request = new Request(apiUrl,
                new List<KeyValuePair<string, string>>
                    {
                             new KeyValuePair<string, string>("compilation_level", level),
                             new KeyValuePair<string, string>("js_code", jsContent),
                             new KeyValuePair<string, string>("output_format", "xml"),
                             new KeyValuePair<string, string>("output_info", "compiled_code"),
                        });
        }

        /// <summary>
        /// Sends the request and returns the optimized code.
        /// </summary>
        /// <returns>The optimized code</returns>
        public string GetCode()
        {
            var responseText = request.Send();
            var xe = XElement.Parse(responseText);
            var code = xe.Element("compiledCode");
            if (code != null)
                return code.Value;
            var error = xe.Element("serverErrors");
            throw new DotCommonException(error == null
                ? "Unknown error occurred when request code from Closure"
                : error.Value);
        }
    }
}
